/**
 * 
 */
package org.hotpotmaterial.codegenn2.controller.v1.admin;

import org.hotpotmaterial.codegenn2.common.ResultCodeConstants;
import org.hotpotmaterial.codegenn2.dto.ResultBaseDTO;
import org.hotpotmaterial.codegenn2.dto.request.FileRequestDTO;
import org.hotpotmaterial.codegenn2.dto.request.StringRequestDTO;
import org.hotpotmaterial.codegenn2.service.IPluginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import io.swagger.annotations.Api;

/**
 * @author Wenxing
 *
 */
@Controller("adminPluginController")
@RequestMapping("/hmteam/codegen/admin/v1/plugins")
@Api(value = "Admin插件Controller", tags={"Admin插件管理"})
public class PluginController {

	@Autowired
	private IPluginService pluginService;

	/**
	 * 根据模板名称更新模板
	 * 
	 * @param pluginName
	 * @param data
	 * @return
	 */
	@PutMapping("/{pluginName}/templates/update")
	public ResponseEntity<ResultBaseDTO> putPluginTemplatesUpdate(@PathVariable String pluginName,
			@RequestBody StringRequestDTO data) {
		pluginService.updateTemplate(pluginName, data.getParam(), data.getParamExt1());
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}

	/**
	 * 根据模板名称上传模板
	 * 
	 * @param pluginName
	 * @param data
	 * @return
	 */
	@PostMapping("/{pluginName}/templates/upload")
	public ResponseEntity<ResultBaseDTO> postPluginTemplatesUpload(@PathVariable String pluginName,
			FileRequestDTO data) {
		pluginService.uploadTemplate(pluginName, data.getFileName(), data.getFile());
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}

	/**
	 * 根据模板名称Zip批量上传
	 * 
	 * @param pluginName
	 * @param data
	 * @return
	 */
	@PostMapping("/{pluginName}/templates/zip_upload")
	public ResponseEntity<ResultBaseDTO> postPluginTemplatesZipUpload(@PathVariable String pluginName,
			FileRequestDTO data) {
		pluginService.uploadAndUnzipTemplates(pluginName, data.getFile());
		return new ResponseEntity<>(
				new ResultBaseDTO().code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS),
				HttpStatus.OK);
	}

}
